package com.lanou.zakerproject.disport;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;

/**
 * Created by dllo on 16/1/12.
 */
public class DisportExAdapter extends BaseExpandableListAdapter {
    private Context context;
    private DisportData disportData;

    public DisportExAdapter(Context context, DisportData disportData) {
        this.context = context;
        this.disportData = disportData;
    }

    @Override
    public int getGroupCount() {
        return disportData.getData().getColumns().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return disportData.getData().getColumns().get(groupPosition).getItems().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_hotspot_ex_group, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.ex_group_tv.setText(disportData.getData().getColumns().get(groupPosition).getTitle());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_hotspot_ex_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (disportData.getData().getColumns().get(groupPosition).getTitle().equals("好玩周报")
                ||disportData.getData().getColumns().get(groupPosition).getTitle().equals("有礼派")) {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(childViewHolder.ex_child_img, R.mipmap.ic_launcher, R.mipmap.default_img_nonet);
            VolleySingleton.getImageLoader().get(disportData.getData().getColumns().get(groupPosition).getItems().get(childPosition).getPic().getUrl(), listener);
            childViewHolder.ex_child_title.setVisibility(View.GONE);
            childViewHolder.ex_child_subTitle.setVisibility(View.GONE);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(childViewHolder.ex_child_img, R.mipmap.ic_launcher, R.mipmap.default_img_nonet);
            VolleySingleton.getImageLoader().get(disportData.getData().getColumns().get(groupPosition).getItems().get(childPosition).getPic().getUrl(), listener);
            childViewHolder.ex_child_title.setText(disportData.getData().getColumns().get(groupPosition).getItems().get(childPosition).getContent());
            childViewHolder.ex_child_subTitle.setText(disportData.getData().getColumns().get(groupPosition).getItems().get(childPosition).getShare_content());
            childViewHolder.ex_child_title.setVisibility(View.VISIBLE);
            childViewHolder.ex_child_subTitle.setVisibility(View.VISIBLE);
        }
        childViewHolder.ex_child_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent();
                intent1.setClass(context, DisportDetailActivity.class);
                String disport_detail_title = disportData.getData().getColumns().get(groupPosition).getTitle();
                String disport_detail_url = disportData.getData().getColumns().get(groupPosition).getItems().get(childPosition).getArticle().getWeburl();
                intent1.putExtra("disport_detail_url", disport_detail_url);
                intent1.putExtra("disport_detail_title", disport_detail_title);
                context.startActivity(intent1);
            }
        });
        return convertView;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder {
        TextView ex_group_tv;

        public GroupViewHolder(View view) {
            ex_group_tv = (TextView) view.findViewById(R.id.item_hotspot_ex_group_tv);
        }
    }

    class ChildViewHolder {
        private ImageView ex_child_img;
        private TextView ex_child_title, ex_child_subTitle;

        public ChildViewHolder(View view) {
            ex_child_img = (ImageView) view.findViewById(R.id.item_hotspot_ex_child_img);
            ex_child_title = (TextView) view.findViewById(R.id.item_hotspot_ex_child_title);
            ex_child_subTitle = (TextView) view.findViewById(R.id.item_hotspot_ex_child_subtitle);


        }
    }
}
